<template>
  <div>
    <div class="box">
      <p class="title">用户信息修改</p>
      <el-form :model="info" status-icon ref="form" label-width="110px" :rules="rules">
        <el-form-item label="用户id" prop="masterID">
          <el-input v-model="info.masterID"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="trueName">
          <el-input v-model="info.trueName"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input maxlength="11" v-model="info.mobile"></el-input>
        </el-form-item>
        <el-form-item label="修改密码" prop="password">
          <el-input placeholder="如需修改密码，请输入新的密码（密码不能小于6位字符，大于20位字符）" type="password" v-model="info.password"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model.number="info.type" placeholder="请选择">
            <el-option label="代理商" :value="1"></el-option>
            <el-option label="供应商" :value="2"></el-option>
            <el-option label="管理员" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model.number="info.status" placeholder="请选择">
            <el-option label="启用" :value="1"></el-option>
            <el-option label="禁用" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input placeholder="请输入详细地址" v-model="info.address"></el-input>
        </el-form-item>
        <el-form-item label="纬度" prop="longitude">
          <el-input placeholder="请打开地图获取纬度" :disabled="true"  v-model="info.longitude"></el-input>
        </el-form-item>
        <el-form-item label="经度" prop="latitude">
          <el-input placeholder="请打开地图获取经度" :disabled="true"  v-model="info.latitude"></el-input>
        </el-form-item>
        <p style="margin-bottom:20px"><span class="open-btn" @click="mapShow = true">打开地图选择</span></p>
        <!-- <el-form-item label="城市" prop="areaCode">
          <el-input v-model="info.areaCode" style="display:none"></el-input>
          <el-input v-model="info.cityName" style="display:none"></el-input>
          <el-input v-model="info.adCode" style="display:none"></el-input>
          <el-cascader
            :options="areaList"
            expand-trigger="hover"
            @change="areaChange"
            v-model="areaSelect"
          >
          </el-cascader>
        </el-form-item> -->
        <el-form-item class="all-handle-btn-box">
          <el-button type="primary" @click="handleCertain(info)">确定</el-button>
          <router-link class="back-btn" to="/storer/list"><el-button>返回</el-button></router-link>
        </el-form-item>
      </el-form>
    </div>
    <div class="all-flex-center pop-black" v-show="mapShow">
      <div class="map-box">
        <div id="map"></div>
        <div class="all-flex-center search-box">
          <input placeholder="请输入搜索地址" type="text" v-model="place">
          <span @click="search">搜索</span>
        </div>
        <div id="panel"></div>
        <p class="notice">*如果搜索结果没有，可通过点击地图中的位置来获取经纬度即可</p>
        <div class="result">{{mapInfo.address}}（经度：{{mapInfo.longitude ? mapInfo.longitude : "-"}} 纬度：{{mapInfo.latitude ? mapInfo.latitude : "-"}}）</div>
        <div class="handle">
          <el-button type="primary" @click="mapInfoCertain">确定</el-button>
          <el-button @click="mapInfoCancel">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {  
  data() {
    return {
      // areaList: [],
      // areaSelect: [],
      mapInfo: {
        address: '',
        longitude: '',
        latitude: '',
        areaCode: ''
      },
      mapShow: false,
      info: {},
      place: '',
      rules: {
        masterID: {required: true, message: "用户id不能为空", trigger: "change"},
        trueName: {required: true, message: "真实姓名不能为空", trigger: "change"},
        mobile: {required: true, pattern: /^\d{7,11}$/, message: "手机号码格式有误", trigger: "change"},
        type: {required: true, message: '类型不能为空', trigger: 'change'},
        status: {required: true, message: "状态不能为空", trigger: "change"},
        address: {required: true, message: "地址不能为空", trigger: "change"},
        longitude: {required: true, pattern: /^(\d|\.)+$/, message: "经度格式有误", trigger: "change"},
        latitude: {required: true, pattern: /^(\d|\.)+$/, message: "纬度格式有误", trigger: "change"}
      }
    }
  },
  created() {
    const _this = this;
    if (!window.sessionStorage.masterData) {
      this.$router.push("/storer/list");
    }
    let data = JSON.parse(window.sessionStorage.masterData);
    this.info = data;
    this.info.password = "";
    // this.areaSelect[0] = this.info.adCode;
    // this.areaSelect[1] = this.info.areaCode ? this.info.areaCode : "";
    this.$nextTick(() => {
      let map = new AMap.Map("map", {
          resizeEnable: true,
          center: [116.397428, 39.90923],//地图中心点
          zoom: 13,//地图显示的缩放级别
          keyboardEnable: false
      });
      //加载经纬定位
      AMap.plugin('AMap.Geocoder', function() {
        _this.geocoder = new AMap.Geocoder();
      });
      //搜索查询
      AMap.plugin(['AMap.PlaceSearch'],function(){
        //构造地点查询类
        let placeSearch = new AMap.PlaceSearch({ 
            pageSize: 5,      // 单页显示结果条数
            pageIndex: 1,     // 页码
            panel: "panel",
            map: map,         // 展现结果的地图实例
            autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
        });
        //点击搜索结果列表返回地址信息
        placeSearch.on("listElementClick", function(res) {
          _this.mapInfo = {
            address: res.data.name,
            longitude: res.data.location.P,
            latitude: res.data.location.R,
            areaCode: res.data.citycode
          };
        });
        //点击热点返回地址信息
        placeSearch.on("markerClick", function(res) {
          _this.mapInfo = {
            address: res.data.name,
            longitude: res.data.location.P,
            latitude: res.data.location.R,
            areaCode: res.data.areaCode
          };
        });
        _this.placeSearch = placeSearch;
      });
      //点击地图任意位置，返回坐标
      map.on("click", function(res) {
        _this.geocoder.getAddress([res.lnglat.lng, res.lnglat.lat], function(status, result) {
          if (status === 'complete' && result.info === 'OK') {
            _this.mapInfo = {
              address: result.regeocode.formattedAddress,
              longitude: res.lnglat.P,
              latitude: res.lnglat.R,
              areaCode: result.regeocode.addressComponent.citycode
            };
          }
        });
      });
    });
  },
  methods: {
    handleCertain(info) {
      const _this = this;
      this.$refs.form.validate(bool => {
        if (bool) {
          this.$confirm("确定要保存么?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消"
          }).then(() => {
            //修改提交
            this.$ajax({
              url: 'master/edit',
              data: {
                app: info
              },
              success(res) {
                if (res.data.code === 1) {
                  _this.$message({
                    message: res.data.msg,
                    type: 'success',
                    onClose(){
                      _this.$router.push("/storer/list");
                    }
                  });
                } else {
                  _this.$message({
                    message: res.data.msg,
                    type: 'warning'
                  });
                }
              }
            });
          }).catch(() => {});
        }
      });
    },
    search() {
      //关键字查询
      this.placeSearch.search(this.place);
    },
    mapInfoCertain() {
      this.$set(this.$data.info, "areaCode", this.mapInfo.areaCode);
      this.$set(this.$data.info, "address", this.mapInfo.address);
      this.$set(this.$data.info, "longitude", this.mapInfo.longitude);
      this.$set(this.$data.info, "latitude", this.mapInfo.latitude);
      this.mapShow = false;
    },
    mapInfoCancel() {
      this.mapInfo = {};
      this.mapShow = false;
    }
  }
};
</script>

<style scoped>
.el-form {
  width: 50%;
  margin: 0 auto;
}
.title {
  padding: 20px 0;
  font-size: 20px;
  text-align: center;
}
.back-btn {
  margin-left: 10px;
}
.open-btn {
  margin-left: 110px;
  color: #409EFF;
  font-size: 15px;
  cursor: pointer;
}
.pop-black {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 2;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
}
.map-box {
  position: relative;
  width: 800px;
  height: 580px;
  background: #fff;
  border-radius: 5px;
}
#map {
  width: 750px;
  height: 400px;
  margin: 25px auto 0;
  box-shadow: 0 0 1px 1px #f1f1f1;
}
.search-box {
  position: absolute;
  top: 25px;
  right: 25px;
  width: 250px;
  height: 40px;
  background: #f1f1f1;
}
.search-box input {
  flex: 1;
  width: 1px;
  padding: 0 5px;
  height: 25px;
  font-size: 15px;
  margin: 0 10px;
}
.search-box span {
  width: 45px;
  height: 25px;
  margin-right: 10px;
  color: #fff;
  font-size: 15px;
  line-height: 25px;
  text-align: center;
  background: #409EFF;
  border-radius: 5px;
}
#panel {
  position: absolute;
  background-color: white;
  max-height: 350px;
  overflow-y: auto;
  top: 65px;
  right: 25px;
  width: 250px;
}
.notice {
  margin-top: 10px;
  padding: 0 25px;
  color: red;
  font-size: 14px;
}
.result {
  padding: 10px 25px;
  font-size: 16px;
}
.handle {
  margin-top: 10px;
  text-align: center;
}
</style>
